

package com.kimo.shop.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kimo.shop.bean.model.Form;
import com.kimo.shop.bean.model.FormItem;
import com.kimo.shop.common.config.Constant;
import com.kimo.shop.common.exception.KimoShopBindException;
import com.kimo.shop.common.util.PageParam;
import com.kimo.shop.service.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author kimo-taozhouchuan on 2018/11/26.
 */
@RestController
@RequestMapping("/admin/form")
public class FormController {

    @Autowired
    private FormService formService;


    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Form>> page(Form form, PageParam<Form> page) {
        IPage<Form> formIPage = formService.page(page, new LambdaQueryWrapper<Form>()
                .eq(Form::getShopId, Constant.PLATFORM_SHOP_ID)
                .like(StrUtil.isNotBlank(form.getFormName()),Form::getFormName,form.getFormName())
                .eq(Objects.nonNull(form.getTimeType()),Form::getTimeType,form.getTimeType())
                .orderByAsc(Form::getSeq)
        );
        return ResponseEntity.ok(formIPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{formId}")
    public ResponseEntity<Form> info(@PathVariable("formId") Long formId) {
        Form form = formService.getById(formId);
        return ResponseEntity.ok(form);
    }

    /**
     * 保存
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('admin:form:save')")
    public ResponseEntity<Void> save(@RequestBody @Valid Form form) {
        Date date = new Date();
        form.setCreateTime(date);
        form.setUpdateTime(date);
        if (!form.getRecommendForm()){
            Long shopId = Constant.PLATFORM_SHOP_ID;
            form.setShopId(shopId);
        }
        formService.save(form);
        //如果是推荐报表，则清除缓存
        if (form.getRecommendForm()){
            formService.removeCache();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('admin:form:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid Form form) {
        if (!form.getRecommendForm()){
            Form formDB = formService.getById(form.getFormId());
            if (!Objects.equals(formDB.getShopId(), Constant.PLATFORM_SHOP_ID)){
                // 该报表数据有误，请刷新后重新输入
                throw new KimoShopBindException("kimo.form.data.error");
            }
        }
        form.setUpdateTime(new Date());
        formService.saveOrUpdate(form);
        //如果是推荐报表，则清除缓存
        if (form.getRecommendForm()){
            formService.removeCache();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{formId}")
    @PreAuthorize("@pms.hasPermission('admin:form:delete')")
    public ResponseEntity<Void> delete(@PathVariable("formId") Long formId) {
        Form formDB = formService.getById(formId);
        formService.removeById(formId);
        //如果是推荐报表，则清除缓存
        if (Objects.isNull(formDB.getShopId())){
            formService.removeCache();
        }
        return ResponseEntity.ok().build();
    }

    /**
     * 获取报表项列表
     */
    @GetMapping("/getFormItem")
    public ResponseEntity<List<FormItem>> getFormItem() {
        List<FormItem> formItemEnumList = formService.getFormItem();
        return ResponseEntity.ok(formItemEnumList);
    }

    /**
     * 生成报表
     * @param formId
     * @param response
     */
    @GetMapping("/formExcel")
    @PreAuthorize("@pms.hasPermission('admin:form:excel')")
    public void formExcel(@RequestParam("formId") Long formId, HttpServletResponse response) {
        formService.formExcel(formId,response);
    }

    /**
     * 分页获取推荐报表
     */
    @GetMapping("/getRecommendFormPage")
    public ResponseEntity<IPage<Form>> getRecommendFormPage(Form form, PageParam<Form> page) {
        IPage<Form> formIPage = formService.getRecommendFormPage(page,form);
        return ResponseEntity.ok(formIPage);
    }

    /**
     * 获取推荐报表
     */
    @GetMapping("/getRecommendFormList")
    public ResponseEntity<List<Form>> getRecommendFormList() {
        List<Form> formList = formService.getRecommendFormList();
        return ResponseEntity.ok(formList);
    }
}
